<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼宇名称" prop="buildingname">
        <el-input
          v-model="queryParams.buildingname"
          placeholder="请输入楼宇名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼宇位置" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入楼宇位置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼宇高度" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入楼宇高度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="占地面积" prop="totalarea">
        <el-input
          v-model="queryParams.totalarea"
          placeholder="请输入占地面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['czj:building:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['czj:building:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['czj:building:remove']"
        >删除</el-button>
      </el-col>
-->

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['czj:building:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buildingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="楼宇id" align="center" prop="buildingid" />
      <el-table-column label="楼宇名称" align="center" prop="buildingname" >
      <template slot-scope="scope">
          <router-link :to="'/realestate/floor/' + scope.row.buildingid" class="link-type">
            <span>{{ scope.row.buildingname }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="楼宇位置" align="center" prop="location" />
      <el-table-column label="楼宇高度" align="center" prop="height" />
      <el-table-column label="占地面积" align="center" prop="totalarea" />
      <el-table-column label="楼宇类型" align="center" prop="buildingtype" >
       <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_buildroom_status" :value="scope.row.buildingtype"/>
        </template>
      </el-table-column>
      <el-table-column label="使用中" align="center" prop="statusUse" />
      <el-table-column label="空闲" align="center" prop="statusVacant" />
      <el-table-column label="维修中" align="center" prop="statusRepair" />
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['czj:building:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['czj:building:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTongji(scope.row)"
            v-hasPermi="['czj:building:remove']"
          >统计</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼宇管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼宇名称" prop="buildingname">
          <el-input v-model="form.buildingname" placeholder="请输入楼宇名称" />
        </el-form-item>
        <el-form-item label="楼宇位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入楼宇位置" />
        </el-form-item>
        <el-form-item label="楼宇高度" prop="height">
          <el-input v-model="form.height" placeholder="请输入楼宇高度" />
        </el-form-item>
        <el-form-item label="占地面积" prop="totalarea">
          <el-input v-model="form.totalarea" placeholder="请输入占地面积" />
        </el-form-item>
        <el-form-item label="使用中" prop="statusUse">
          <el-input v-model="form.statusUse" placeholder="请输入使用中" />
        </el-form-item>
        <el-form-item label="空闲" prop="statusVacant">
          <el-input v-model="form.statusVacant" placeholder="请输入空闲" />
        </el-form-item>
        <el-form-item label="维修中" prop="statusRepair">
          <el-input v-model="form.statusRepair" placeholder="请输入维修中" />
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>




    
    <!-- 统计图 -->
<el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
  <div ref="chart" style="width: 100%; height: 300px;"></div>

  <div slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
  </div>
</el-dialog>

  </div>
</template>

<script>
import { listBuilding, getBuilding, delBuilding, addBuilding, updateBuilding } from "@/api/czj/building";
import * as echarts from 'echarts';

export default {
  name: "Building",
  dicts: ['sys_buildroom_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼宇管理表格数据
      buildingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingname: null,
        location: null,
        height: null,
        totalarea: null,
        buildingtype: null,
        statusUse: null,
        statusVacant: null,
        statusRepair: null,
        createdby: null,
        createdtime: null,
        updatedby: null,
        updatedtime: null,
        deletedby: null,
        deletedtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildingname: [
          { required: true, message: "楼宇名称不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "楼宇位置不能为空", trigger: "blur" }
        ],
    }
    };
  },
  created() {
    this.getList();
    
  },
  methods: {

    initChart(data) {
  this.$nextTick(() => {
    const chartDom = this.$refs.chart;
    const myChart = echarts.init(chartDom);

    const option = {
      title: {
        text: '楼宇房间状态统计',
        subtext: '统计数据展示',
        left: 'center'
      },
      tooltip: {
        trigger: 'item',
        formatter: '{a} <br/>{b}: {c} ({d}%)'
      },
      legend: {
        orient: 'vertical',
        left: 'left',
        data: ['使用中', '空闲', '维修中']
      },
      series: [
        {
          name: '楼宇状态',
          type: 'pie',
          radius: '55%',
          center: ['50%', '60%'],
          data: [
            { value: data.statusUse, name: '使用中' },
            { value: data.statusVacant, name: '空闲' },
            { value: data.statusRepair, name: '维修中' }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };

    // 使用刚指定的配置项和数据显示图表
    myChart.setOption(option);
  });
},


    /** 查询楼宇管理列表 */
    getList() {
      this.loading = true;
      listBuilding(this.queryParams).then(response => {
        console.log(response.rows);
        this.buildingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        buildingid: null,
        buildingname: null,
        location: null,
        height: null,
        totalarea: null,
        buildingtype: null,
        statusUse: null,
        statusVacant: null,
        statusRepair: null,
        createdby: null,
        createdtime: null,
        updatedby: null,
        updatedtime: null,
        deletedby: null,
        deletedtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.buildingid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼宇管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const buildingid = row.buildingid || this.ids
      getBuilding(buildingid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼宇管理";
      });
    },
    /**统计按钮*/
     handleTongji(row) {
      this.reset();
      const buildingid = row.buildingid || this.ids
      getBuilding(buildingid).then(response => {
        this.form = response.data;
        this.open1 = true;
        this.title = "统计楼宇房间状态管理";

        // 初始化统计图
    this.initChart(response.data);
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buildingid != null) {
            updateBuilding(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuilding(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buildingids = row.buildingid || this.ids;
      this.$modal.confirm('是否确认删除楼宇管理编号为"' + buildingids + '"的数据项？').then(function() {
        return delBuilding(buildingids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('czj/building/export', {
        ...this.queryParams
      }, `building_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
